/**
 * Created by zhaojm on 15/3/21.
 */
// hero
game.Hero = cc.Class.extend({
    _stateTag_enum : {
        run : 0,
        run_fast : 1,
        jump : 2,
        strike : 3,

    },

    _road_x_list : [
        130,
        175,
        220
    ],

    _defaultJumpSpeedY : 300,
    _defaultGravity : -1200,

    _currentRoad : null,


    _spriteSheet : null,
    _sprite : null,

    _action_run : null,
    _action_runfast : null,
    _action_jump : null,

    _layer : null,


    _stateTag : null,

    _pinconeCount : null,   // 吃的坚果数量，用来计数，出现加速药瓶

    _defaultY : null,



    _jumpSpeedY : null,
    _currentPos : null,
    _gravity : null,

    ctor:function(){

        var size = cc.winSize;
        game._Globals.hero = this;

        this._pinconeCount = 0;
        this._defaultY = size.height * 0.4;

        this._initAction();


        this._sprite = new cc.Sprite('#hero/run/0.png');
        this._sprite.setScale(0.8);

        this._sprite.setAnchorPoint(cc.p(0.5, 0));

        //this._sprite.setPositionY();
        // 初始化在中间
        this._currentRoad = 1;
        this._currentPos = cc.p(this._road_x_list[this._currentRoad], this._defaultY);
        //this._sprite.setPositionX();
        this._jumpSpeedY = this._defaultJumpSpeedY;
        this._gravity = this._defaultGravity;


        //physics
        //var contentSize = this._sprite.getContentSize();
        //this._body = new cp.Body(1, cp.momentForBox(Number.POSITIVE_INFINITY, contentSize.width, contentSize.height));
        //body.applyImpulse(cp.v(this.runningSpeed, 0), cp.v(0, 0));
        //body.applyForce(cp.v(0, 150), cp.v(0, 0));
        //this._body.p = cc.p(this._road_x_list[this._currentRoad], size.height * 0.6);
        //this._sprite.setBody(this._body);
        //this._shape = new cp.BoxShape(this._body, contentSize.width - 14, contentSize.height-18);
        //this._shape = shape;
        //this._shape.setElasticity(0);
        //this._shape.setCollisionType(SpriteTag.bird);
        //Sensors only call collision callbacks, and never generate real collisions
        //this._shape.setSensor(true);

        this._sprite.setPosition(this._currentPos);

    },


    addToLayer : function(layer, spriteSheet){
        this._spriteSheet = spriteSheet;
        this._spriteSheet.addChild(this._sprite, 3);
        //layer.addChild(this._spriteSheet);
        this._layer = layer;
        //this._space = space;
        //space.addBody(this._body);
        //space.addShape(this._shape);
        this.run();

    },

    _initAction : function(){
        this._action_run = new cc.RepeatForever(
            new cc.Sequence(
                new cc.Animate(
                    new cc.Animation([0, 1, 2, 3, 4, 5].map(function (i) {
                        return cc.spriteFrameCache.getSpriteFrame("hero/run/" + i + ".png");
                    }), 0.07)
                ),
                new cc.CallFunc(this.afterRun)
            )
        );
        this._action_runfast = new cc.RepeatForever(
            new cc.Sequence(
                new cc.Animate(
                    new cc.Animation([0, 1, 2, 3, 4, 5].map(function (i) {
                        return cc.spriteFrameCache.getSpriteFrame("hero/run_fast/" + i + ".png");
                    }), 0.07)
                ),
                new cc.CallFunc(this.afterRunFast)
            )
        );
        this._action_jump = new cc.Sequence(
            new cc.Animate(
                new cc.Animation([0, 1, 2].map(function (i) {
                    return cc.spriteFrameCache.getSpriteFrame("hero/jump/" + i + ".png");
                }), this._defaultJumpSpeedY * 2 / - this._defaultGravity)
            ),
            new cc.CallFunc(this.afterJump)
        );

    },

    moveRight : function(){
        if(this._stateTag != this._stateTag_enum.jump) {
            this._currentRoad++;
            if (this._currentRoad >= this._road_x_list.length) {
                this._currentRoad = this._road_x_list.length - 1;
            }
            this._sprite.setPositionX(this._road_x_list[this._currentRoad]);
        }
    },
    moveLeft : function(){
        //cc.log('move left');
        if(this._stateTag != this._stateTag_enum.jump) {
            this._currentRoad--;
            if (this._currentRoad <= 0) {
                this._currentRoad = 0;
            }
            this._sprite.setPositionX(this._road_x_list[this._currentRoad]);
        }
    },




    run : function(){
        cc.log('run..');
        this._jumpSpeedY = 0;
        //this._gravity = 0;
        if(this._stateTag != this._stateTag_enum.run){
            this._sprite.stopAllActions();
            this._stateTag = this._stateTag_enum.run;
            this._sprite.runAction(this._action_run);
        }

    },

    runFast : function(){
        cc.log('run fast');
        this._jumpSpeedY = 0;
        //this._gravity = 0;
        if(this._stateTag != this._stateTag_enum.run_fast){
            this._sprite.stopAllActions();
            this._stateTag = this._stateTag_enum.run_fast;
            this._sprite.runAction(this._action_runfast);
        }

    },

    jump : function(){
        cc.log('jump,,,stateTag = ', this._stateTag);
        if(this._stateTag == this._stateTag_enum.run || this._stateTag == this._stateTag_enum.run_fast){
            this._sprite.stopAllActions();

            this._sprite.runAction(this._action_jump);
        //    this._body.setVel(cp.v(this._body.getVel().x, this._defaultJumpVel));
            this._gravity = this._defaultGravity;
            this._jumpSpeedY = this._defaultJumpSpeedY;
            this._stateTag = this._stateTag_enum.jump;

        }else{}



    },

    strike : function(){
        cc.log('strike');
        // 碰撞，
        if(this._layer._speedUpTimeCount <= 0 && (this._stateTag == this._stateTag_enum.run || this._stateTag == this._stateTag_enum.run_fast)) {
            this._jumpSpeedY = 0;
            this._stateTag = this._stateTag_enum.strike;
            this._sprite.stopAllActions();
            var jumpUp = new cc.JumpBy(0.8, cc.p(0, 0), 40, 2);
            this._sprite.setSpriteFrame('hero/strike/0.png');
            var seq = new cc.Sequence(jumpUp, new cc.CallFunc(this.afterStrike));
            this._sprite.runAction(seq);
        }

    },

    afterStrike : function(){
        cc.log('afterStrike');

        var self = game._Globals.hero;
        if(self._layer._bear.getScale() >= 0.6){
            self.runFast();
        }else {
            self.run();
        }


    },



    afterJump:function(){
        //cc.log('afterJump');
    },

    afterRun : function(){
        //cc.log('after run');
        var self = game._Globals.hero;
        if(self._layer._bear.getScale() >= 0.6){
            self._sprite.stopAllActions();
            self._sprite.runAction(self._action_runfast);
        }
    },
    afterRunFast : function(){
        //cc.log('after run fast');
        var self = game._Globals.hero;
        if(self._layer._bear.getScale() < 0.6){
            self._sprite.stopAllActions();
            self._sprite.runAction(self._action_run);
        }
    },


    update : function(dt){
        //if(!this.isCollided){
        if(this._stateTag == this._stateTag_enum.jump) {
            this._gravity = this._defaultGravity;
            this._currentPos.y = 1 / 2 * this._gravity * dt * dt + this._jumpSpeedY * dt + this._currentPos.y;
            this._jumpSpeedY = this._jumpSpeedY + this._gravity * dt;

            if (this._currentPos.y <= this._defaultY) {

                this._currentPos.y = this._defaultY;

                if(this._jumpSpeedY <= 0){

                    if (this._layer._bear.getScale() >= 0.6) {
                        this.runFast();
                    } else {
                        this.run();
                    }
                }


            }

            this._sprite.setPositionY(this._currentPos.y);

        }else if(this._stateTag == this._stateTag_enum.run || this._stateTag == this._stateTag_enum.run_fast){
            this._sprite.setPositionY(this._defaultY);
        }
    },


    getPosition:function(){
        return this._sprite.getPosition();
    },
    getPositionX:function(){
        return this._sprite.getPositionX();
    },

    gameOver:function(){
        this._sprite.stopAllActions();
    },


});